Pandas中的时间日期转换 | 您所在的位置:网站首页 › excel 时间转换为时间戳 › Pandas中的时间日期转换 |
小伙伴们大家好,相信大家在使用pandas的过程中一定避免不了类型转换的问题,尤其是时间类型的转换,这里我尽可能用最短的篇幅帮大家速通pandas中的时间类型转换问题。 将数字转换成datetime将时间戳的数字转换为datetime类型 df = pd.DataFrame({'date': [1470195805, 1480195805, 1674725925]})image.png1.使用to_datetime函数我们调用to_datetime()函数,并传入参数 unit='s' image.png2.使用astype函数df['date'].astype('datetime64[s]')image.png 这里datetime64位NumPy类型,常见单位如下: CodeMeaningYyearMmonthDdayhhourmminutessecondmsmillisecondnsnanosecond将字符串转换为datetime在pandas中,string以object的形式出现。无论使用to_datetime还是astype函数都可以完成字符串到时间日期的转换。 df = pd.DataFrame({'date':['3/10/2019','3/11/2020','3/12/2021']}) image.png1.使用to_datetime函数pd.to_datetime(df['date']) |
CopyRight 2018-2019 实验室设备网 版权所有 |